#ifndef __M_LISTVIEW_H__
#define __M_LISTVIEW_H__

#include <stdint.h>

#include <vita2d.h>

#include "file.h"

typedef struct vita2d_texture Image;

enum LayoutVisibilityType
{
    LAYOUT_VISIBILITY_VISIBLE,
    LAYOUT_VISIBILITY_INVISIBLE,
    LAYOUT_VISIBILITY_GONE,
};

enum LayoutViewType
{
    LAYOUT_VIEW_UNKONW,
    LAYOUT_VIEW_TEXT,
    LAYOUT_VIEW_IMAGE,
    LAYOUT_VIEW_CHECKBOX,
    LAYOUT_VIEW_RADIOBUTTON,
};

typedef struct
{
    int visibility;
} LayoutParams;

typedef struct
{
    uint32_t bg_color;
    uint32_t clicked_color;
} LayoutColors;

typedef struct
{
    int layout_sx;
    int layout_sy;
    int layout_dx;
    int layout_dy;
    int layout_width;
    int layout_height;
    int padding_left;
    int padding_top;
    int padding_right;
    int padding_bottom;
    int inside_sx;
    int inside_sy;
    int inside_dx;
    int inside_dy;
    int inside_width;
    int inside_height;
} LayoutLocations;

typedef struct
{
    int scroll_enable;
    int scroll_reset;
    int scroll_finished;
    int scroll_x_current;
    int scroll_y_current;
    int scroll_x_start;
    int scroll_y_start;
    int scroll_x_final;
    int scroll_y_final;
    int scroll_x_step;
    int scroll_y_step;
    int scroll_count;
} LayoutScrolls;

typedef struct
{
    int current;
    int pressed;
    int released;
    int current_count;
    int released_count;
    int onclick;
    int onlongclick;
} LayoutClicks;

typedef void (*LayoutEventCallback)(void *view);

typedef struct
{
    LayoutEventCallback onClick;
    LayoutEventCallback onLongClick;
} LayoutEvents;

typedef struct
{
    int scroll_enable;
    int scroll_reset;
    int scroll_x_off;
    int scroll_count;
} TextScrolls;

typedef struct
{
    char *text;
    int text_width;
    int text_height;
    float text_size;
    uint32_t text_color;
    TextScrolls scrolls;
} TextViewDatas;

typedef struct
{
    LayoutParams params;
    LayoutLocations locations;
    LayoutColors colors;
    LayoutScrolls scrolls;
    LayoutClicks clicks;
    LayoutEvents events;
    TextViewDatas datas;
} TextView;

typedef struct
{
    Image *image;
    int image_width;
    int image_height;
    float x_scale;
    float y_scale;
} ImageViewDatas;

typedef struct
{
    LayoutParams params;
    LayoutLocations locations;
    LayoutColors colors;
    LayoutScrolls scrolls;
    LayoutClicks clicks;
    LayoutEvents events;
    ImageViewDatas datas;
} ImageView;

typedef struct
{
    Image *images[2];
    float x_scale;
    float y_scale;
    int is_checked;
} CheckBoxViewDatas;

typedef struct
{
    LayoutParams params;
    LayoutLocations locations;
    LayoutColors colors;
    LayoutScrolls scrolls;
    LayoutClicks clicks;
    LayoutEvents events;
    CheckBoxViewDatas datas;
} CheckBoxView;

typedef struct
{
    ImageView imageView;
    TextView nameTextView;
    TextView infoTextView;
    CheckBoxView checkBoxView;
    LayoutParams params;
    LayoutLocations locations;
    LayoutColors colors;
    LayoutClicks clicks;
} ItemView;

typedef struct _ListView ListView;
typedef void (*ListViewOnClick)(ListView *listView);

typedef struct
{
    ListViewOnClick onItemClick;
    ListViewOnClick onItemLongClick;
} ListViewEvents;

typedef int (*ListViewGetListLengh)(void *list);
typedef void *(*ListViewGetEntryByNumber)(void *list, int index);
typedef void *(*ListViewGetNextEntry)(void *list, void *entry, int index);
typedef int (*ListViewGetImageViewData)(ImageView *view, void *list, void *entry, int index);
typedef int (*ListViewGetNameViewData)(TextView *view, void *list, void *entry, int index);
typedef int (*ListViewGetInfoViewData)(TextView *view, void *list, void *entry, int index);
typedef int (*ListViewGetCheckBoxViewData)(CheckBoxView *view, void *list, void *entry, int index);

typedef struct
{
    ListViewGetListLengh getListLengh;
    ListViewGetEntryByNumber getEntryByNumber;
    ListViewGetNextEntry getNextEntry;
    ListViewGetImageViewData getImageViewData;
    ListViewGetNameViewData getNameViewData;
    ListViewGetInfoViewData getInfoViewData;
    ListViewGetCheckBoxViewData getCheckBoxViewData;
} ListViewCallbacks;

typedef struct
{
    int layout_sx;
    int layout_sy;
    int layout_width;
    int layout_height;
    int padding_left;
    int padding_top;
    int padding_right;
    int padding_bottom;
    uint32_t listview_bg_color;
    uint32_t itemview_bg_color;
    int enable_info;
    int enable_image;
    int enable_checkBox;
    int disable_divider;
} ListViewParams;

typedef struct
{
    int gamepad_mode;
    int draw_top_pos;
    int top_pos;
    int focus_pos;
    int scroll_step_pixls;
    int inertia_scroll_pixls;
    int inertia_ori_scroll_pixls;
    int real_current_sy;
    int real_final_sy;
    int real_min_sy;
    int real_min_dy;
    int real_max_sy;
    int real_max_dy;
    int real_max_height;
    int draw_lines;
} ListViewStates;

typedef struct _ListView
{
    void *list;
    ItemView itemView;
    TextView dividerView;
    LayoutLocations locations;
    ListViewStates states;
    ListViewParams params;
    LayoutClicks clicks;
    ListViewEvents evens;
    ListViewCallbacks callbacks;
} ListView;

int ListViewGetTopPos(ListView *listView);
int ListViewGetFocusPos(ListView *listView);
int ListViewGetDrawTopPos(ListView *listView);
int ListViewGetDrawLines(ListView *listView);

int ListViewMoveFocusPos(ListView *listView, int type);

int ListViewGetTouchMode(ListView *listView);
void ListViewSetGamepadMode(ListView *listView, int mode);

int ListViewIsScrolling(ListView *listView);
void ListViewStopScroll(ListView *listView);

void ListViewSetOnItemClikListener(ListView *listView, ListViewOnClick onItemClick);
void ListViewSetOnItemLongClikListener(ListView *listView, ListViewOnClick onItemLongClick);

int ListViewGetTopItemViewSyOffset(ListView *listView);
void ListViewSetTopItemViewSyOffset(ListView *listView, int y_offset);
int ListViewGetTopItemViewSy(ListView *listView);

void ListViewSetLayoutXY(ListView *listView, int x, int y);

void ListViewAlignTopItem(ListView *listView);
void ListViewAlignBottomItem(ListView *listView);

void ListViewRefreshPosByTopPos(ListView *listView, int top_pos, int focus_pos, int scroll_type);
void ListViewRefreshPosByFocusPos(ListView *listView, int top_pos, int focus_pos, int scroll_type);

void ListViewScrollUp(ListView *listView, int n_item);
void ListViewScrollDown(ListView *listView, int n_item);

void ListViewScrollToFirst(ListView *listView);
void ListViewScrollToLast(ListView *listView);

void ListViewRefreshList(ListView *listView, void *list);

ListView *NewListView(ListViewParams *params, ListViewCallbacks *callbacks, void *list);
void DestroyListView(ListView *listView);

void ListViewDraw(ListView *listView);
void ListViewEvent(ListView *listView);

#endif
